﻿using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 2d音效管理
/// </summary>
public class Effect2DSound : ISound 
{   
    /// <summary>
    /// 当前正在播放列表
    /// </summary>
    List<SoundObj> workAudioList = new List<SoundObj>();
    /// <summary>
    /// 空闲列表
    /// </summary>
    List<AudioSource> ldleAudioList = new List<AudioSource>();

    public Effect2DSound()
    {
        Mute = false;
        workAudioList.Clear();
        ldleAudioList.Clear();
    }
    
    public override bool Mute
    {
        get { return base.Mute; }
        set
        {
            if (base.Mute != value)
            {
                base.Mute = value;
                for (int i = workAudioList.Count - 1; i >= 0; i--)
                {
                    workAudioList[i].audioSource.mute = value;
                }
            }
        }
    }

    public override AudioSource Play(Table_SoundTempItem soundTemp, AudioClip clip)
    {
        AudioSource audioSource = null; SoundObj soundObj = null;
        if (soundTemp.PlayType == 1)
        {
            soundObj = GetObjByID(soundTemp.Id);
            //遇到同一音效id时，停止播放上一条，开始播放下一条。
            if (soundObj == null)
            {
                audioSource = SetAudio(soundTemp, clip);
            }
            else
            {
                audioSource = soundObj.audioSource;
            }
            audioSource.time = 0;
        }
        else
        {
            //遇到同一音效id时，继续播放上一条，并开始播放下一条。
            audioSource = SetAudio(soundTemp, clip);
        }
        audioSource.Play();
        audioSource.pitch = 1;
        return audioSource;
    }


    public override void Pause()
    {
        for (int i = workAudioList.Count - 1; i >= 0; i--)
        {
            workAudioList[i].audioSource.Pause();
        }
    }

    public override void RePlay()
    {
        for (int i = workAudioList.Count - 1; i >= 0; i--)
        {
            workAudioList[i].audioSource.Play();
        }
    }

    public override void Stop()
    {
        for (int i = workAudioList.Count - 1; i >= 0; i--)
        {
            SoundObj obj = workAudioList[i];
            workAudioList.RemoveAt(i);
            ClearAudio(obj.audioSource);
        }
    }

    public override void StopById(int id)
    {
        SoundObj soundObj = GetObjByID(id);
        if (soundObj != null)
        {
            workAudioList.Remove(soundObj);
            ClearAudio(soundObj.audioSource);
        }
    }

    public override void SetPitch(int id, float pitch)
    {
        SoundObj soundObj = GetObjByID(id);
        if (soundObj != null)
        {
            soundObj.audioSource.pitch = pitch;
        }
    }

    public override void Update()
    {
        for (int i = workAudioList.Count - 1; i >= 0; i--)
        {
            var item = workAudioList[i];
            if (item.audioSource.time == 0 && !item.audioSource.isPlaying 
                && !item.audioSource.loop)
            {
                workAudioList.RemoveAt(i);
                //播放完成 返回到对象池
                ClearAudio(item.audioSource);
            }
        }
    }

    private AudioSource SetAudio(Table_SoundTempItem soundTemp, AudioClip clip)
    {
        var audioSource = GetAudio();
        audioSource.clip = clip;
        audioSource.loop = soundTemp.IsLoop;
        audioSource.volume = soundTemp.Volume;
        audioSource.mute = Mute;

        var soundObj = new SoundObj();
        soundObj.id = soundTemp.Id;
        soundObj.audioSource = audioSource;
        workAudioList.Add(soundObj);
        return audioSource;
    }

    private void ClearAudio(AudioSource audioSource)
    {
        audioSource.Stop();
        audioSource.clip = null;
        audioSource.enabled = false;
        audioSource.time = 0;
        ldleAudioList.Add(audioSource);
    }

    private AudioSource GetAudio()
    {
        AudioSource audioSource;
        if (ldleAudioList.Count > 0)
        {
            audioSource = ldleAudioList[0];
            audioSource.enabled = true;
            ldleAudioList.RemoveAt(0);
        }
        else
        {
            audioSource = AllUpdate.UIRoot.AddComponent<AudioSource>();
            audioSource.playOnAwake = false;
        }
        return audioSource;
    }

    private SoundObj GetObjByID(int id)
    {
        for (int i = 0; i < workAudioList.Count; i++)
        {
            if (workAudioList[i].id == id)
            {
                return workAudioList[i];
            }
        }
        return null;
    }

    public override bool IsPlaying(int id)
    {
        SoundObj obj = GetObjByID(id);
        if (obj != null)
        {
            return obj.audioSource.isPlaying;
        }
        return false;
    }
}